package dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Admin;
import pojo.CheckInRecord;
import pojo.CheckOutRecord;
import utils.JDBCUtils;
import utils.SystemEnum;

import java.sql.*;
import java.util.ArrayList;

public class CheckOutRecordDaoImpl implements CheckOutRecordDao{

    //调换信息
    private Log log = LogFactory.getLog(getClass());

    @Override
    public ArrayList<CheckOutRecord> findAll() {
        log.info(SystemEnum.BEGIN);
        ArrayList<CheckOutRecord> list = new ArrayList<CheckOutRecord>();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM check_out_record ";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while(rs.next()){
                CheckOutRecord checkOutRecord = new CheckOutRecord(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getDate(4),rs.getString(5));
                list.add(checkOutRecord);
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return list;
    }

    @Override
    public CheckOutRecord findById(Integer stuId) {
        log.info(SystemEnum.BEGIN);
        CheckOutRecord checkOutRecord = new CheckOutRecord();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM check_in_record WHERE stuId = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,stuId);
            rs = statement.executeQuery();
            while(rs.next()){
                checkOutRecord.setStuId(rs.getInt(1));
                checkOutRecord.setDormId(rs.getInt(2));
                checkOutRecord.setBedId(rs.getInt(3));
                checkOutRecord.setActual_cherk_out_time(rs.getDate(4));
                checkOutRecord.setReason(rs.getString(5));
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return checkOutRecord;
    }

    @Override
    public int insert(Connection con,CheckOutRecord checkOutRecord) throws SQLException {
        log.info(SystemEnum.BEGIN);
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            String sql = "INSERT INTO check_in_record VALUES (?,?,?,?,?)";
            statement = con.prepareStatement(sql);
            statement.setInt(1,checkOutRecord.getStuId());
            statement.setInt(2,checkOutRecord.getStuId());
            statement.setInt(3,checkOutRecord.getBedId());
            statement.setDate(4,new java.sql.Date(checkOutRecord.getActual_cherk_out_time().getTime()));
            statement.setString(5,checkOutRecord.getReason());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
            throw e;
        }finally {
            JDBCUtils.close(null,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }

    @Override
    public int update(CheckOutRecord checkOutRecord) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "UPDATE check_out_record SET dormId = ?,bedId = ?,actual_cherk_out_time=?,reason=? WHERE stuId = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,checkOutRecord.getDormId());
            statement.setInt(2,checkOutRecord.getBedId());
            statement.setDate(3,new java.sql.Date(checkOutRecord.getActual_cherk_out_time().getTime()));
            statement.setString(4,checkOutRecord.getReason());
            statement.setInt(5,checkOutRecord.getStuId());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;

    }

    @Override
    public int delete(CheckOutRecord checkOutRecord) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "DELETE FROM check_out_record  WHERE stuId = ? ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,checkOutRecord.getStuId());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }
}
